#!/bin/bash
# Download 8 dynamically-generated salt/key constants and write to wp-keys.php
#
# by Tim Moody tim@timmoody.com

DEST={{ wp_abs_path }}/wp-keys.php    # /library/wordpress
BACKUP={{ wp_abs_path }}/wp-keys.php.BAK

echo '<?php' > $DEST
curl https://api.wordpress.org/secret-key/1.1/salt/ >> $DEST
RC=$?

# if the download of keys failed, revert to previous version
if [ $RC -ne 0 ]; then
    cp $BACKUP $DEST
else
    echo '?>' >> $DEST
    chown root:{{ apache_user }} $DEST
    chmod 640 $DEST
    cp -f $DEST $BACKUP
fi
